package com.edu.controller;


import com.edu.entity.Chapter;
import com.edu.entity.chapter.ChapterVo;
import com.edu.entity.vo.CourseQuery;
import com.edu.service.ChapterService;
import com.edu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author mxs
 * @since 2021-10-21
 */
@RestController
@RequestMapping("/edu/chapter")
@CrossOrigin
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    /**
     * 根据课程id获取所有章节信息
     *
     * @param courseId 课程id
     * @return
     */
    @GetMapping("/getChapterVideoByCourseId/{courseId}")
    public R getChapterVideoByCourseId(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoVoByCourseId(courseId);
        return R.success().data("list", list);
    }

    /**
     * 添加课程
     *
     * @param chapter
     * @return
     */
    @PostMapping("/addChpater")
    public R addChapter(@RequestBody Chapter chapter) {
        chapterService.save(chapter);
        return  R.success();
    }

    /**
     * 查询章节id
     * @param chapterId
     * @return
     */
    @GetMapping("/getChapter/{chapterId}")
    public R getChapter(@PathVariable String chapterId){
        Chapter chapter = chapterService.getById(chapterId);
        return R.success().data("chapter",chapter);
    }

    /**
     * 更新章节信息
     * @param chapter
     * @return
     */
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody Chapter chapter){
        boolean update = chapterService.updateById(chapter);
        System.out.println(update);
        return R.success();
    }

    /**
     * 根据课程id删除章节，如果课程有小节那么就不进行删除
     * @param chapterId
     * @return
     */
    @GetMapping("/deleteChpater/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        Boolean update = chapterService.deleteChapter(chapterId);
        if (!update){
            return R.error().message("请先将所有小节先删除！");
        }
        return R.success();
    }


}

